package com.zja.www.controller;

import com.zja.www.bean.Result;
import com.zja.www.entity.User;
import com.zja.www.service.DoctorService;
import com.zja.www.service.MedicalRecordService;
import com.zja.www.service.UserService;


import java.util.List;

/**
 * @author ja
 * @create 2025/3/31
 * @description 用户controller控制层
 */
public class UserController extends BaseController {
    public UserController(UserService userService,
                          DoctorService doctorService,
                          MedicalRecordService medicalRecordService) {
        super(userService, doctorService,  medicalRecordService);
    }

    /**
     * 处理用户登录
     */
    public Result handleLogin() {
        System.out.print("请输入用户名：");
        String username = readStringInput();
        System.out.print("请输入密码：");
        String password = readStringInput();

        Result result = userService.login(username, password);
        if (result.isSuccess()) {
            // 登录成功，返回用户名以便View层获取用户信息
            User user = userService.getUserByUsername(username);
            return Result.success("登录成功", user);
        }
        return result;
    }

    /**
     * 处理用户注册
     */
    public Result handleRegister() {
        System.out.print("请输入用户名：");
        String username = readStringInput();

        String password = readPasswordWithConfirm();

        System.out.print("请输入学号：");
        int studentId = readIntInput();

        System.out.print("请输入手机号：");
        String phone = readStringInput();

        User user = new User(0, username, password, studentId, phone);
        boolean success = userService.addUser(user);
        return success ? Result.success("注册成功", null)
                : Result.fail("注册失败，用户名可能已存在");
    }

    /**
     * 获取所有用户
     */
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    /**
     * 更新密码
     */
    public Result updatePassword(int id, String newPassword) {
        User user = userService.getUserById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        user.setPassword(newPassword);
        boolean success = userService.updateUser(user);
        return success ? Result.success("密码更新成功", null) : Result.fail("密码更新失败");
    }

    /**
     * 更新手机号
     */
    public Result updatePhone(int id, String newPhone) {
        User user = userService.getUserById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        user.setPhone(newPhone);
        boolean success = userService.updateUser(user);
        return success ? Result.success("手机号更新成功", null) : Result.fail("手机号更新失败");
    }



    /**
     * 删除用户
     */
    public Result deleteUser(int id) {
        boolean success = userService.deleteUser(id);
        return success ? Result.success("删除成功", null)
                : Result.fail("删除失败，用户可能不存在");
    }



    public User getUserByUsername(String username) {
        return userService.getUserByUsername(username);
    }

}
